package com.jc.subject.domain.handler;

import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.common.enums.SubjectTypeFlagEnum;
import com.jc.subject.domain.convert.SubjectAnswerConverter;
import com.jc.subject.domain.convert.SubjectHandlerConverter;
import com.jc.subject.domain.entity.SubjectAnswerDO;
import com.jc.subject.domain.entity.SubjectInfoDO;
import com.jc.subject.domain.entity.SubjectOptionDO;
import com.jc.subject.infra.basic.entity.SubjectBrief;
import com.jc.subject.infra.basic.entity.SubjectMultiple;
import com.jc.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author gongjiawei
 * @Date 2024/7/9 14:32
 * @注释
 */
@Component
public class SubjectMultipleHandler implements SubjectStrategy{

    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectTypeFlagEnum getcode() {
        return SubjectTypeFlagEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoDO subjectInfoDO) {
        //将信息插入单选表中 TODO
        List<SubjectAnswerDO> optionList = subjectInfoDO.getOptionList();
        if (optionList.isEmpty())return;
        List<SubjectMultiple> subjectMultiples = SubjectAnswerConverter.INSTANCE.convertBoToMultipleList(optionList);
        subjectMultiples.forEach(subjectBrief -> {
            subjectBrief.setSubjectId(subjectInfoDO.getId());
            subjectBrief.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        });
        subjectMultipleService.batchInsert(subjectMultiples);
    }

    @Override
    public SubjectOptionDO query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerDO> subjectAnswerBOList = SubjectHandlerConverter.INSTANCE.convertMultipleEntityToBoList(result);
        SubjectOptionDO subjectOptionBO = new SubjectOptionDO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
